﻿using MediatR;
using SlaughterItem.Common.Domains.Detection;
using SlaughterItem.Common.Dtos;
using SlaughterItem.Common.Infrastructures.Interfaces;
using SlaughterItem.Enum;

namespace SlaughterItem.API.Application.Commands.Week02.COA
{
    public class DelCOADataHandler : IRequestHandler<DelCOADataCommand, Result>
    {
        public DelCOADataHandler(IBaseRepository<COAData> cOADataRepository)
        {
            COADataRepository = cOADataRepository;
        }

        public IBaseRepository<COAData> COADataRepository { get; set; }
        public async Task<Result> Handle(DelCOADataCommand request, CancellationToken cancellationToken)
        {
            var result= new Result() { Code=(int)CommonErrorCode.Delete};
            var list = COADataRepository.GetAll().FirstOrDefault(x => x.COAId == request.COAId);
            if (list != null)
            {
                list.ISDeleteCOA = true;
                COADataRepository.Update(list);
                result.Code = (int)CommonErrorCode.Success;
                return result;
            }
            else
            {
                return result;
            }
        }
    }
}
